package study.saas.order.service.Impl;

import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import study.saas.common.dto.CommodityPageDTO;
import study.saas.common.viewmodel.ApiResultV2;
import study.saas.common.viewmodel.PageResult;
import study.saas.order.Mapper.CommodityMapper;
import study.saas.order.entity.Commodity;
import study.saas.order.service.CommodityFacade;
import study.saas.order.service.CommodityService;

import java.util.List;


@Service
public class CommodityServiceImpl implements CommodityService {
    @Autowired
    CommodityMapper commodityMapper;
    @Reference(version = "1.0.0")
    private CommodityFacade commodityFacade;

    //功能一实现类：新增商品
    @Override
    public void add(Commodity commodity) {
        //向商品表插入数据
//        commodityMapper.insert(commodity);
        commodityFacade.save(commodity);
    }

    //功能二实现类：修改商品
    @Override
    public void update(Commodity commodity) {
//        commodityMapper.update(commodity);
        commodityFacade.save(commodity);
    }

    //功能三-一实现类：根据id查询商品
    @Override
    public Commodity getById(Integer id) {
        Commodity commodity = commodityMapper.getById(id);
        return commodity;
    }
    //功能三-二实现类：根据名称查询商品
    @Override
    public Commodity getByName(String commodityName) {
        Commodity commodity = commodityFacade.findCommodity(commodityName);
        return commodity;
    }

//    //功能四实现类：分页查询商品
//    @Override
//    public PageResult getByPage(CommodityPageDTO commodityPageDTO) {
//        PageHelper.startPage(commodityPageDTO.getPage(),commodityPageDTO.getPageSize());
//        Page<Commodity> page = commodityMapper.getByPage(commodityPageDTO);
//        PageResult pageResult = new PageResult(page.getTotal(),page.getResult());
//        return pageResult;
//    }
    //功能四-Jpa实现类：分页查询商品
    @Override
    public PageResult getByPageByJpa(CommodityPageDTO commodityPageDTO) {
        // 创建 Pageable 对象，指定页码和每页大小
        int page = commodityPageDTO.getPage() - 1;  // 由于页码从 0 开始，需要减去 1
        int pageSize = commodityPageDTO.getPageSize();

        // 手动创建 PageRequest 对象
        Pageable pageable = new PageRequest(page, pageSize);  // 使用构造函数创建

        // 分页查询商品
        Page<Commodity> pageQuary = commodityFacade.findByCommodityNameContaining(commodityPageDTO.getCommodityName(), pageable);

        // 将查询结果封装成 PageResult 对象
        PageResult pageResult = new PageResult(pageQuary.getTotalElements(), pageQuary.getContent());
        return pageResult;
    }


    //功能五实现类：上架下架商品
    @Override
    public void updateStatus(Integer status, Integer id) {
        Commodity commodity = Commodity
                .builder()
                .keyId(id)
                .isDeleted(status)
                .build();
        commodityMapper.update(commodity);
    }

    //功能六实现类：修改商品库存
    @Override
    public void updateAmount(Integer amount, Integer id) {
        Commodity commodity = Commodity
                .builder()
                .keyId(id)
                .amount(amount)
                .build();
        commodityMapper.update(commodity);
    }

    //功能七实现类：删除商品(单个/批量)
    @Transactional
    @Override
    public void deleteByIds(List<Integer> ids) {
        //阻止上架商品被删除
        ids.forEach(id -> {
        Commodity commodity = commodityMapper.getById(id);
        if(commodity.getIsDeleted() == 1){
            throw new RuntimeException("商品上架无法删除");
        }});
        //删除未上架商品
        commodityMapper.deleteByIds(ids);

    }

    //功能七-二实现类：根据商品名称删除商品
    @Override
    public void deleteByName(String commodityName) {
        commodityFacade.deleteCommodity(commodityName);
    }

    //JPA--减少商品数量实现类
    public ApiResultV2<Commodity> decreaseCommodity(String commodityName) {
        // 查找商品
        Commodity commodity = commodityFacade.findCommodity(commodityName);

        if (commodity == null) {
            return new ApiResultV2<>("error", null, "商品未找到");  // 商品不存在时的返回
        }

        if (commodity.getAmount() <= 0) {
            return new ApiResultV2<>("error", null, "商品数量不足");  // 商品数量不足时的返回
        }

        // 减少商品数量
        commodity.setAmount(commodity.getAmount() - 1);

        // 保存更新后的商品
        commodityFacade.save(commodity);

        return new ApiResultV2<>("success", commodity, "商品数量减少成功");  // 返回成功的结果
    }

    //JPA--增加商品数量实现类
    public ApiResultV2<Commodity> increaseCommodity(String commodityName, int quantity) {
        // 根据商品名称查找商品
        Commodity commodity = commodityFacade.findCommodity(commodityName);

        // 如果商品不存在
        if (commodity == null) {
            return new ApiResultV2<>("error", null, "商品未找到");
        }

        // 增加商品数量
        commodity.setAmount(commodity.getAmount() + quantity);

        // 保存更新后的商品
        commodityFacade.save(commodity);

        // 返回成功结果
        return new ApiResultV2<>("success", commodity, "商品数量增加成功");
    }
}

